package crm.salesOrderProcessing.managedbean;

import commonInfrastructure.accountManagement.entity.EmployeeEntity;
import commonInfrastructure.accountManagement.session.EmployeeSessionBean;
import javax.ejb.EJB;
import crm.clientManagement.entity.CompanyEntity;
import javax.faces.bean.ManagedBean;
import java.util.List;
import crm.clientManagement.session.CompanySessionBean;
import java.io.Serializable;
import java.util.ArrayList;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

@ManagedBean
@ViewScoped
public class TestGetCompanyManagedBean implements Serializable{
  
     
    @EJB
    CompanySessionBean companySessionBean;
    @EJB
    EmployeeSessionBean employee;
    private CompanyEntity selectedCompany;

    public TestGetCompanyManagedBean () {
        selectedCompany=new CompanyEntity();
    }
    

    public List <String> getCompanyNames() {
        
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        Long loginId = (Long) request.getSession().getAttribute("userId");
        EmployeeEntity user = employee.getEmployeeById(loginId);
        List<String> userType = new ArrayList<String>();
        for (int i=0;i<user.getRoles().size();i++){
            userType.add(user.getRoles().get(i).getRoleName());
        }
        List<CompanyEntity> companyList;
        if (userType.contains("Sales Manager")){
            companyList = companySessionBean.getAllCompanies();
        }
        else{
            companyList = companySessionBean.getCompaniesBySalesman(loginId);
        }
        
        List<String> nameList = new ArrayList<String>();
        for (int j=0;j<companyList.size();j++){
            nameList.add(companyList.get(j).getName());
        }
        
        return nameList; 
    }

    public CompanyEntity getSelectedCompany() {
        return selectedCompany;
    }

    public void setSelectedCompany(CompanyEntity selectedCompany) {
        this.selectedCompany = selectedCompany;
    }
}
